package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.io.*;
import java.lang.reflect.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;

/**
 * Action: Rename a proposal. 
 * @author S.N.Fraser
 * $Id: RENAME_PROPOSALImpl.java,v 1.3 2007/02/27 11:49:27 snf Exp $
 */

public class RENAME_PROPOSALImpl extends TransactionImpl implements Logging {
    
    /** The path used to locate the Proposal in the OSS database. */
    protected Path   proposalPath;
   
    protected String newName;

    protected Path   newPath;

    public RENAME_PROPOSALImpl(Path proposalPath) {
	super();
	this.proposalPath     = proposalPath;
	this.newName          = newName;
	this.newPath          = newPath;
    }
    
    public RENAME_PROPOSALImpl(RENAME_PROPOSAL request) {
	super(request);
	this.proposalPath     = request.getProposalPath();	
	this.newName          = request.getNewName();
	this.newPath          = request.getNewPath();
    }
    
    /**
     *
     * @return Boolean or an OSS_Error.
     */
    public TRANSACTION_DONE exec() { 

	Logger logger = LogManager.getLogger("TRACE");
	
	Transaction t = null;
	try {
	    t = Transaction.current();
	  
	    // Locate the Proposal's owner (PI) from its path expression.
	   User user = OSS_Utilities.fetchUser(proposalPath);
	   
	    // Check the PI user exists.
	    if (user == null) {
		logger.log(ERROR, 3, "RENAME_PROPOSAL_impl", id, "exec", 
			   "User for: "+proposalPath.toString()+" Not in database.");		
		return error(TRANSACTION.PATH_RESOLUTION_ERROR,
			     "Could not find PI user for: "
			     +proposalPath.toString());
	    }

	    // Find the new PI - if any.
	    User newUser = null;
	    if (newPath == null) {
		// not relocating...
		newUser = user;

	    } else {
		
		newUser = OSS_Utilities.fetchUser(newPath);

		// Check the new PI user exists.
		if (newUser == null) {
		    logger.log(ERROR, 3, "RENAME_PROPOSAL_impl", id, "exec",
			       "New User "+newPath+" for: "+proposalPath.toString()+" Not in database.");
		    return error(TRANSACTION.PATH_RESOLUTION_ERROR,
				 "Could not find new PI user "+newPath+" for: "
				 +proposalPath.toString());
		}
	    }
	    
	    String proposalId = proposalPath.getProposalByName();
	    
	    Proposal proposal = null;
	    try {
		proposal = OSS_Utilities.fetchProposal(proposalPath);
	    } catch (IOException iox) {
		logger.log(1, CLASS, id, "exec",
			   "Proposal: "+proposalPath.toString()+
			   " Error fetching from Database: "+iox);
		return error(TRANSACTION.DATABASE_READ_ERROR,			   
			     "Proposal: "+proposalPath.toString()+
			     " Error reading from database: "+iox);
	    }		
	    
	    // Remove
	    user.deleteProposal(proposalId);

	    // splice in new location
	    proposal.setName(newName);

	    newUser.addProposal(proposal.getName());
	    proposal.setPath(newUser.getFullPath()); // CHECK THIS 
	    proposal.updateChildren();

	    String oldtu = proposalPath.getTagByName()+"/"+proposalPath.getUserByName();
	    Path nupath = new Path(newUser.getFullPath());
	    String newtu = nupath.getTagByName()+"/"+nupath.getUserByName();
	    proposal.addRevisionInfo(System.currentTimeMillis(), 
				     "Phase2Gui:unknown", 
				     0x12121212, 
				     "Renamed from: "+proposalId+" to "+newName+
				     (newUser != user ? ", relocated from "+oldtu+" to "+newtu : 
				      ", no re-location"));
	    
	    // Store it
	    try {
		OSS_Utilities.storeProposal(proposal);
	    } catch (IOException ix) {
		logger.log(1, CLASS, id, "exec",
			   "Proposal ["+proposal.getFullPath()+
			   "] Unable to Store: "+ix);
		return error(TRANSACTION.DATABASE_UPDATE_ERROR, 
				     "Proposal ["+proposal.getFullPath()+
			     "] Unable to Store: "+ix);
	    }

	} finally {
	    t.commit();
	}

	RENAME_PROPOSAL_DONE done = new RENAME_PROPOSAL_DONE(id);
	done.setSuccessful(true);
	return done;

    }
    
}

/** $Log: RENAME_PROPOSALImpl.java,v $
/** Revision 1.3  2007/02/27 11:49:27  snf
/** added target user
/**
/** Revision 1.2  2007/02/14 09:19:21  snf
/** Added new path info.
/** */
